//
//  ProfileOneViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/10/31.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "ProfileOneViewController.h"
#define cellHeight 44
#define IconWH 70
#define kBorder 10
@interface ProfileOneViewController () <UITextFieldDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UITextField *nameField;
@property (nonatomic, retain) UITextField *schoolField;
@property (nonatomic, retain) UITextField *ageField;
@property (nonatomic, retain) UITextField *mottoField;
@property (nonatomic, retain) UITextField *chooseAddressField;
@property (nonatomic, retain) UITextField *addAddressField;
@property (nonatomic, retain) UIButton *maleBtn;
@property (nonatomic, retain) UIButton *femaleBtn;
@end

@implementation ProfileOneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTop];
    [self initUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:UITextFieldTextDidBeginEditingNotification object:self.nameField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:UITextFieldTextDidBeginEditingNotification object:self.ageField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:UITextFieldTextDidBeginEditingNotification object:self.schoolField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:UITextFieldTextDidBeginEditingNotification object:self.mottoField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:UITextFieldTextDidBeginEditingNotification object:self.chooseAddressField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change) name:UITextFieldTextDidBeginEditingNotification object:self.addAddressField];
    
    //键盘隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    //移除通知响应
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



//TODO:初始化导航栏
- (void)initTop {
    [self addCustomTitleWithTitle:@"基本信息"];
    self.navigationItem.rightBarButtonItem = nil;
}


//TODO:绘制UI
- (void)initUI{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt)];
    [self.view addSubview:self.scrollView];
    
    UIImage *lineImage = [UIImage imageNamed:@"line"];
    
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt/5)];
    UIImageView *head_icon = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-IconWH)/2, (head.frame.size.height-IconWH)/2, IconWH, IconWH)];
    head_icon.image = [UIImage imageNamed:@"head_icon"];
    [head addSubview:head_icon];
    UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, head.frame.size.height, ScreenWidth, 2)];
    line1.image = lineImage;
    [head addSubview:line1];
    [self.scrollView addSubview:head];
    
    UIView *nameView = [[UIView alloc] initWithFrame:CGRectMake(0, head.frame.size.height, ScreenWidth, cellHeight)];
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 60, cellHeight)];
    nameLabel.text = @"姓 名";
    [nameView addSubview:nameLabel];
    self.nameField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(nameLabel.frame), 2, ScreenWidth-60, cellHeight)];
    self.nameField.delegate = self;
    self.nameField.text = @"陈佳慧";
    self.nameField.tag = -1;
    [nameView addSubview:self.nameField];
    UIImageView *line2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, nameView.frame.size.height, ScreenWidth, 2)];
    line2.image = lineImage;
    [nameView addSubview:line2];
    [self.scrollView addSubview:nameView];
    
    UIView *sexView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(nameView.frame)+2, ScreenWidth, cellHeight)];
    UILabel *sexLabel = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 60, cellHeight)];
    sexLabel.text = @"性 别";
    [sexView addSubview:sexLabel];
    self.maleBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(sexLabel.frame), 2, 100, cellHeight-2*2)];
    [self.maleBtn setBackgroundImage:[UIImage imageNamed:@"male-pitch"] forState:UIControlStateNormal];
    [self.maleBtn addTarget:self action:@selector(maleSelected) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:self.maleBtn];
    self.femaleBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.maleBtn.frame)+kBorder, 2, 100, cellHeight-2*2)];
    [self.femaleBtn setBackgroundImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
    [self.femaleBtn addTarget:self action:@selector(femaleSelected) forControlEvents:UIControlEventTouchUpInside];
    [sexView addSubview:self.femaleBtn];
    UIImageView *line3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, sexView.frame.size.height, ScreenWidth, 2)];
    line3.image = lineImage;
    [sexView addSubview:line3];
    [self.scrollView addSubview:sexView];
    
    UIView *ageView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(sexView.frame)+2, ScreenWidth, cellHeight)];
    UILabel *ageLabel = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 60, cellHeight)];
    ageLabel.text = @"年 龄";
    [ageView addSubview:ageLabel];
    self.ageField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(ageLabel.frame), 2, ScreenWidth-60, cellHeight)];
    self.ageField.text = @"20";
    self.ageField.tag = 0;
    self.ageField.delegate = self;
    [ageView addSubview:self.ageField];
    
    UIDatePicker *picker = [[UIDatePicker alloc] init];
    picker.datePickerMode = UIDatePickerModeDate;
    picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    self.ageField.inputView = picker;
    
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.frame = CGRectMake(0, 0, ScreenWidth, cellHeight);
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    //点击toolbar上面的完成 只退出键盘 右上角依然有完成
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(hidenKeyboard)];
    toolbar.items = @[item1,item2];
    self.ageField.inputAccessoryView = toolbar;
    
    UIImageView *line4 = [[UIImageView alloc] initWithFrame:CGRectMake(0, ageView.frame.size.height, ScreenWidth, 2)];
    line4.image = lineImage;
    [ageView addSubview:line4];
    [self.scrollView addSubview:ageView];
    
    UIView *subjectView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(ageView.frame)+2, ScreenWidth, cellHeight)];
    UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 60, cellHeight)];
    subjectLabel.text = @"学 科";
    [subjectView addSubview:subjectLabel];
    UIImageView *subImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(subjectLabel.frame), 4, 140, cellHeight-2*4)];
    subImageView.image = [UIImage imageNamed:@"subject_backgroud"];
    UILabel *subDetail = [[UILabel alloc]initWithFrame:CGRectMake(1, 0, 110, subImageView.frame.size.height)];
    subDetail.text = @"小学六年级语文";
    subDetail.font = [UIFont systemFontOfSize:15];
    [subDetail setTextColor:[UIColor whiteColor]];
    [subImageView addSubview:subDetail];
    UIButton *deletBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(subDetail.frame)+2, 7, 20, 20)];
    [deletBtn setBackgroundImage:[UIImage imageNamed:@"corss"] forState:UIControlStateNormal];
    [subImageView addSubview:deletBtn];
    [subjectView addSubview:subImageView];
    UIImageView *line5 = [[UIImageView alloc] initWithFrame:CGRectMake(0, subjectView.frame.size.height, ScreenWidth, 2)];
    line5.image = lineImage;
    [subjectView addSubview:line5];
    [self.scrollView addSubview:subjectView];
    
    UIView *addressView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(subjectView.frame)+2, ScreenWidth, 2*cellHeight)];
    UILabel *addressLable = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, cellHeight/2, 60, cellHeight)];
    addressLable.text = @"地 址";
    [addressView addSubview:addressLable];
    UIImageView *addressImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addressLable.frame)+20, 6, 30, 30)];
    addressImageView.image = [UIImage imageNamed:@"address"];
    [addressView addSubview:addressImageView];
    self.chooseAddressField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addressImageView.frame)+3, 2, 170, cellHeight)];
    self.chooseAddressField.delegate = self;
    self.chooseAddressField.tag = 3;
    self.chooseAddressField.placeholder = @"请选择小区/写字楼/学校等";
    [self.chooseAddressField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.chooseAddressField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [addressView addSubview:self.chooseAddressField];
//    UILabel *holderLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addressImageView.frame)+3, 0, 170, cellHeight)];
//    holderLabel.text = @"请选择小区/写字楼/学校等";
//    holderLabel.font = [UIFont systemFontOfSize:14];
//    [holderLabel setTextColor:[UIColor grayColor]];
//    [addressView addSubview:holderLabel];
    
//    UIButton *forwardBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(holderLabel.frame), kBorder+1, 15, 20)];
    UIButton *forwardBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.chooseAddressField.frame), kBorder+1, 15, 20)];
    [forwardBtn setBackgroundImage:[UIImage imageNamed:@"forward"] forState:UIControlStateNormal];
    [addressView addSubview:forwardBtn];
    UIImageView *middleLine = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addressLable.frame)+20, cellHeight, ScreenWidth-CGRectGetMaxX(addressLable.frame)-20, 2)];
    middleLine.image = lineImage;
    [addressView addSubview:middleLine];
    
//    UILabel *holderLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addressLable.frame)+20, cellHeight+2, 240, cellHeight)];
//    holderLabel2.text = @"补充详细地址（如门牌号/楼层号等）";
//    holderLabel2.font = [UIFont systemFontOfSize:14];
//    [holderLabel2 setTextColor:[UIColor grayColor]];
//    [addressView addSubview:holderLabel2];
    self.addAddressField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(addressLable.frame)+20, cellHeight+2, 240, cellHeight)];
    self.addAddressField.delegate = self;
    self.addAddressField.tag = 4;
    self.addAddressField.placeholder = @"补充详细地址（如门牌号/楼层号等）";
    [self.addAddressField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.addAddressField setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    [addressView addSubview:self.addAddressField];
    UIImageView *line6 = [[UIImageView alloc] initWithFrame:CGRectMake(0, addressView.frame.size.height, ScreenWidth, 2)];
    line6.image = lineImage;
    [addressView addSubview:line6];
    [self.scrollView addSubview:addressView];
    
    UIView *schoolView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(addressView.frame)+2, ScreenWidth, cellHeight)];
    UILabel *schoolLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 60, cellHeight/2)];
    schoolLabel1.text = @"毕业学校";
    schoolLabel1.font = [UIFont systemFontOfSize:13];
    [schoolView addSubview:schoolLabel1];
    UILabel *schoolLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, CGRectGetMaxY(schoolLabel1.frame), 60, cellHeight/2)];
    schoolLabel2.text = @"/所在学校";
    schoolLabel2.font = [UIFont systemFontOfSize:13];
    [schoolView addSubview:schoolLabel2];
    self.schoolField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(schoolLabel2.frame)+20, 2, ScreenWidth-60, cellHeight)];
    self.schoolField.text = @"浙江大学";
    self.schoolField.delegate = self;
    self.schoolField.tag = 1;
    [schoolView addSubview:self.schoolField];
    UIImageView *line7 = [[UIImageView alloc] initWithFrame:CGRectMake(0, schoolView.frame.size.height, ScreenWidth, 2)];
    line7.image = lineImage;
    [schoolView addSubview:line7];
    [self.scrollView addSubview:schoolView];
    
    UIView *mottoView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(schoolView.frame)+2, ScreenWidth, cellHeight)];
    UILabel *mottoLabel = [[UILabel alloc] initWithFrame:CGRectMake(kBorder, 0, 60, cellHeight)];
    mottoLabel.text = @"座右铭";
    [mottoView addSubview:mottoLabel];
    self.mottoField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(mottoLabel.frame)+20, 2, ScreenWidth-60, cellHeight)];
    self.mottoField.text = @"面朝大海，春暖花开";
    self.mottoField.tag = 2;
    self.mottoField.delegate = self;
    [mottoView addSubview:self.mottoField];
    [self.scrollView addSubview:mottoView];
    
    //点击背景时 键盘收回
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyboard)];
    [self.scrollView addGestureRecognizer:tapGesture];
    
}

- (void)doBack {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

/*
 当UITextFieldText DidBeginEditingNotification
 */
- (void)change{
    [self addCustomTitleWithTitle:@"修改基本信息"];
    
    UIButton *finish = [UIButton buttonWithType:UIButtonTypeCustom];
    finish.frame = CGRectMake(ScreenWidth-30, 20, 50, 30);
    finish.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [finish setTitle:@"完成" forState:UIControlStateNormal];
    [finish setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [finish addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *finishItem = [[UIBarButtonItem alloc] initWithCustomView:finish];
    self.navigationItem.rightBarButtonItem = finishItem;
}

//点击右上角完成 触摸背景时 收回键盘
- (void)finish {
    [self initTop];
    [self hidenKeyboard];
}

/*
 性别选择男
 */
- (void)maleSelected {
    [self.maleBtn setBackgroundImage:[UIImage imageNamed:@"male-pitch"] forState:UIControlStateNormal];
    [self.femaleBtn setBackgroundImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
}

/*
 性别选择女
 */
- (void)femaleSelected {
    [self.femaleBtn setBackgroundImage:[UIImage imageNamed:@"female-pitch"] forState:UIControlStateNormal];
    [self.maleBtn setBackgroundImage:[UIImage imageNamed:@"male"] forState:UIControlStateNormal];
}

#pragma mark 解决虚拟键盘挡住UITextField的方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    int offset;
    if(textField.tag == 0) //年龄
        offset = 0;
    else if(textField.tag == 1) //学校
        offset = -140;
    else if(textField.tag == 2) //座右铭
        offset = -188;
    else if(textField.tag == 3) //选择地址
        offset = -80;
    else if(textField.tag == 4) //补充地址
        offset = -100;
    else if(textField.tag == -1) //姓名
        offset = 64;
    CGRect rect=CGRectMake(0.0f,offset,ScreenWidth,ScreenHeihgt);
    self.view.frame=rect;
    [UIView commitAnimations];
    return YES;
}

//TODO:恢复原始视图位置
-(void)resumeView
{
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect=CGRectMake(0.0f,64,ScreenWidth,ScreenHeihgt);
    self.view.frame=rect;
    [UIView commitAnimations];
}


//TODO:隐藏键盘的方法
-(void)hidenKeyboard
{
//    DLog(@"hidenKeyboard");
    [self.view endEditing:YES];
    [self resumeView];
}

//TODO:通知响应 键盘即将隐藏时调用
- (void)keyboardWillHiden {
    [self resumeView];
//    DLog(@"keyboardWillHiden");
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self finish];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
